package com.zscat.mallplus.pms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.cms.service.ICmsPrefrenceAreaProductRelationService;
import com.zscat.mallplus.cms.service.ICmsSubjectProductRelationService;
import com.zscat.mallplus.entity.vo.GoodsTypeVo;
import com.zscat.mallplus.entity.vo.ShortageVo;
import com.zscat.mallplus.pms.entity.*;
import com.zscat.mallplus.pms.mapper.*;
import com.zscat.mallplus.pms.service.*;
import com.zscat.mallplus.pms.vo.PmsProductParam;
import com.zscat.mallplus.pms.vo.PmsProductResult;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.ums.service.RedisService;
import com.zscat.mallplus.util.DateUtils;
import com.zscat.mallplus.util.HttpClientUtil;
import com.zscat.mallplus.util.JsonUtil;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.IdWorker;
import com.zscat.mallplus.utils.ValidatorUtils;
import com.zscat.mallplus.vo.Rediskey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-19
 */
@Slf4j
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements IPmsProductService {

    @Resource
    private PmsProductMapper productMapper;
    @Resource
    private IPmsMemberPriceService memberPriceDao;
    @Resource
    private PmsMemberPriceMapper memberPriceMapper;
    @Resource
    private IPmsProductLadderService productLadderDao;
    @Resource
    private PmsProductLadderMapper productLadderMapper;
    @Resource
    private IPmsProductFullReductionService productFullReductionDao;
    @Resource
    private PmsProductFullReductionMapper productFullReductionMapper;
    @Resource
    private IPmsSkuStockService skuStockDao;
    @Resource
    private PmsSkuStockMapper pmsSkuStockMapper;
    @Resource
    private PmsSkuStockMapper skuStockMapper;

    @Resource
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Resource
    private PmsProductConsultServiceImpl pmsProductConsultService;

    @Resource
    private IPmsProductAttributeValueService productAttributeValueDao;
    @Resource
    private PmsProductAttributeValueMapper productAttributeValueMapper;
    @Resource
    private ICmsSubjectProductRelationService subjectProductRelationDao;
    @Resource
    private CmsSubjectProductRelationMapper subjectProductRelationMapper;
    @Resource
    private ICmsPrefrenceAreaProductRelationService prefrenceAreaProductRelationDao;
    @Resource
    private CmsPrefrenceAreaProductRelationMapper prefrenceAreaProductRelationMapper;

    @Resource
    private PmsProductVertifyRecordMapper productVertifyRecordDao;

    @Resource
    private PmsProductVertifyRecordMapper productVertifyRecordMapper;
    @Resource
    private RedisService redisService;

    @Override
    public int create(PmsProductParam productParam) {
        int count;
        //创建商品
        PmsProduct product = productParam;
        product.setCreateTime(new Date());
        product.setId(null);
        //处理sku的编码
        handleSkuStockCode(productParam.getSkuStockList(), product);
        if (ValidatorUtils.isEmpty(product.getProductSn())) {
            product.setProductSn(IdWorker.getId() + "");
        }
        if (ValidatorUtils.empty(product.getExpireTime())) {
            product.setExpireTime(DateUtils.strToDate(DateUtils.addDay(new Date(), 5)));
        }
        if (ValidatorUtils.empty(product.getOriginalPrice())) {
            product.setOriginalPrice(product.getPrice());
        }
        if (ValidatorUtils.empty(product.getUnit())) {
            product.setUnit("件");
        }
        SysUser user = UserUtils.getCurrentMember();
        product.setStoreName(user.getStoreName());
        //product.setStoreId(user.getStoreId());
        String albumPics = product.getAlbumPics();

        if (!StringUtils.isEmpty(albumPics)) {
            String[] split = albumPics.split(",");

            if (!split[0].contains(".png") || !split[0].contains(".jpg")) {
                String url = split[1];
                String newUrl = fileUpload(url);
                product.setShareImg("http://192.168.1.51:8085/file/" + newUrl);

            } else {
                String url = split[0];
                String newUrl = fileUpload(url);
                product.setShareImg("http://192.168.1.51:8085/file/" + newUrl);
            }

        }
        product.setPublishTime(new Date());
        productMapper.insert(product);
        //根据促销类型设置价格：、阶梯价格、满减价格
        Long productId = product.getId();
        //会员价格
        //   relateAndInsertList(memberPriceDao, productParam.getMemberPriceList(), productId);
        //阶梯价格
        // relateAndInsertList(productLadderDao, productParam.getProductLadderList(), productId);
        //满减价格
        // relateAndInsertList(productFullReductionDao, productParam.getProductFullReductionList(), productId);

        //添加sku库存信息
        relateAndInsertList(skuStockDao, productParam.getSkuStockList(), productId);
        //添加商品参数,添加自定义商品规格
        relateAndInsertList(productAttributeValueDao, productParam.getProductAttributeValueList(), productId);
        //关联专题
        relateAndInsertList(subjectProductRelationDao, productParam.getSubjectProductRelationList(), productId);
        //关联优选
        relateAndInsertList(prefrenceAreaProductRelationDao, productParam.getPrefrenceAreaProductRelationList(), productId);
        count = 1;
        redisService.set(String.format(Rediskey.GOODSDETAIL, product.getId()), JsonUtil.objectToJson(productParam));
        return count;
    }

    private void handleSkuStockCode(List<PmsSkuStock> skuStockList, PmsProduct product) {
        if (CollectionUtils.isEmpty(skuStockList)) return;
        int stock = 0;
        for (int i = 0; i < skuStockList.size(); i++) {
            PmsSkuStock skuStock = skuStockList.get(i);
            skuStock.setProductName(product.getName());
            if (StringUtils.isEmpty(skuStock.getSkuCode())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                StringBuilder sb = new StringBuilder();
                //日期
                sb.append(sdf.format(new Date()));
                //四位商品id
                sb.append(String.format("%04d", product.getProductCategoryId()));
                //三位索引id
                sb.append(String.format("%03d", i + 1));
                skuStock.setSkuCode(sb.toString());
            }
            stock = stock + skuStock.getStock();
        }
        product.setStock(stock);
    }

    @Override
    public PmsProductResult getUpdateInfo(Long id) {
        return productMapper.getUpdateInfo(id);
    }

    @Override
    public int update(Long id, PmsProductParam productParam) {

        int count;
        //更新商品信息
        PmsProduct product = productParam;
        product.setId(id);
        if (ValidatorUtils.isEmpty(product.getProductSn())) {
            product.setProductSn(IdWorker.getId() + "");
        }
        handleSkuStockCode(productParam.getSkuStockList(), product);

        String albumPics = product.getAlbumPics();

        if (!StringUtils.isEmpty(albumPics)) {
            String[] split = albumPics.split(",");

            if (!split[0].contains(".png") || !split[0].contains(".jpg")) {
                String url = split[1];
                String newUrl = fileUpload(url);
                product.setShareImg("http://192.168.1.51:8085/file/" + newUrl);

            } else {
                String url = split[0];
                String newUrl = fileUpload(url);
                product.setShareImg("http://192.168.1.51:8085/file/" + newUrl);
            }

        }
        productMapper.updateById(product);
        redisService.remove(String.format(Rediskey.GOODSDETAIL, product.getId()));
        //会员价格
        //  memberPriceMapper.delete(new QueryWrapper<>(new PmsMemberPrice()).eq("product_id", id));
        //  relateAndInsertList(memberPriceDao, productParam.getMemberPriceList(), id);
        //阶梯价格

        productLadderMapper.delete(new QueryWrapper<>(new PmsProductLadder()).eq("product_id", id));
        relateAndInsertList(productLadderDao, productParam.getProductLadderList(), id);
        //满减价格

        productFullReductionMapper.delete(new QueryWrapper<>(new PmsProductFullReduction()).eq("product_id", id));
        relateAndInsertList(productFullReductionDao, productParam.getProductFullReductionList(), id);
        //修改sku库存信息
        skuStockMapper.delete(new QueryWrapper<>(new PmsSkuStock()).eq("product_id", id));

        relateAndInsertList(skuStockDao, productParam.getSkuStockList(), id);
        //修改商品参数,添加自定义商品规格

        productAttributeValueMapper.delete(new QueryWrapper<>(new PmsProductAttributeValue()).eq("product_id", id));
        relateAndInsertList(productAttributeValueDao, productParam.getProductAttributeValueList(), id);
        //关联专题

        subjectProductRelationMapper.delete(new QueryWrapper<>(new CmsSubjectProductRelation()).eq("product_id", id));
        relateAndInsertList(subjectProductRelationDao, productParam.getSubjectProductRelationList(), id);
        //关联优选

        prefrenceAreaProductRelationMapper.delete(new QueryWrapper<>(new CmsPrefrenceAreaProductRelation()).eq("product_id", id));
        relateAndInsertList(prefrenceAreaProductRelationDao, productParam.getPrefrenceAreaProductRelationList(), id);
        count = 1;

        redisService.set(String.format(Rediskey.GOODSDETAIL, product.getId()), JsonUtil.objectToJson(productParam));
        return count;
    }


    @Override
    public int updateVerifyStatus(Long ids, Integer verifyStatus, String detail) {
        PmsProduct product = new PmsProduct();
        product.setVerifyStatus(verifyStatus);
        int count = productMapper.update(product, new QueryWrapper<PmsProduct>().eq("id", ids));
        //修改完审核状态后插入审核记录

        PmsProductVertifyRecord record = new PmsProductVertifyRecord();
        record.setProductId(ids);
        record.setCreateTime(new Date());
        record.setDetail(detail);
        record.setStatus(verifyStatus);
        record.setVertifyMan(UserUtils.getCurrentMember().getUsername());
        productVertifyRecordMapper.insert(record);
        redisService.remove(String.format(Rediskey.GOODSDETAIL, product.getId()));
        return count;
    }

    @Override
    public int updatePublishStatus(List<Long> ids, Integer publishStatus) {
        PmsProduct record = new PmsProduct();
        record.setPublishStatus(publishStatus);
        record.setPublishTime(new Date());
        clerGoodsRedis(ids);
        return productMapper.update(record, new QueryWrapper<PmsProduct>().in("id", ids));
    }

    public void clerGoodsRedis(List<Long> ids) {
        for (Long id : ids) {
            redisService.remove(String.format(Rediskey.GOODSDETAIL, id));
        }
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        PmsProduct record = new PmsProduct();
        record.setRecommandStatus(recommendStatus);
        clerGoodsRedis(ids);
        return productMapper.update(record, new QueryWrapper<PmsProduct>().in("id", ids));
    }

    @Override
    public int updateNewStatus(List<Long> ids, Integer newStatus) {
        PmsProduct record = new PmsProduct();
        record.setNewStatus(newStatus);
        clerGoodsRedis(ids);
        return productMapper.update(record, new QueryWrapper<PmsProduct>().in("id", ids));
    }

    @Override
    public int updateDeleteStatus(List<Long> ids, Integer deleteStatus) {
        PmsProduct record = new PmsProduct();
        record.setDeleteStatus(deleteStatus);
        clerGoodsRedis(ids);
        return productMapper.update(record, new QueryWrapper<PmsProduct>().in("id", ids));
    }

    @Override
    public List<PmsProduct> list(String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("delete_status", 0);

        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("name", keyword);

        }
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsProductVertifyRecord> getProductVertifyRecord(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", id);

        return productVertifyRecordMapper.selectList(queryWrapper);
    }

    @Override
    public List<ShortageVo> shortageList(String keyword, Integer pageIndex, Integer pageSize) {
        //筛选

        List<ShortageVo> result = new ArrayList<>();


        List<PmsSkuStock> skuStocks = findSkustockList("product_name", keyword, "low_stock", "stock", pageIndex, pageSize);

        //说明有缺货的
        if (skuStocks.size() > 0) {

            for (PmsSkuStock pmsSkuStock : skuStocks) {

                ShortageVo shortageVo = new ShortageVo();

                Long productId = pmsSkuStock.getProductId();

                findCategoryName(productId);

                PmsProduct pmsProduct = productMapper.selectById(productId);

                Long productCategoryId = pmsProduct.getProductCategoryId();

                PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(productCategoryId);

                Long parentId = pmsProductCategory.getParentId();

                PmsProductCategory pmsProductCategory2 = pmsProductCategoryMapper.selectById(parentId);

                Long parentId1 = pmsProductCategory2.getParentId();

                PmsProductCategory pmsProductCategory3 = pmsProductCategoryMapper.selectById(parentId1);

                String name = pmsProductCategory3.getName();
                String name1 = pmsProductCategory2.getName();
                String name2 = pmsProductCategory.getName();

                shortageVo.setGoodsName(pmsProduct.getName());
                shortageVo.setGoodsCategory(name2 + name1 + name);
                shortageVo.setGoodsAttr(pmsSkuStock.getSp1() + pmsSkuStock.getSp2() + pmsSkuStock.getSp3() + pmsSkuStock.getSp4());
                shortageVo.setId(pmsSkuStock.getId());
                shortageVo.setStock(pmsSkuStock.getStock());
                shortageVo.setPrice(pmsSkuStock.getPrice());
                shortageVo.setGoodsImg(pmsSkuStock.getPic());

                result.add(shortageVo);
            }

        }
        return result;
    }

    @Override
    public boolean isChinese(String keyword) {
        for (int i = 0; i < keyword.length(); i = i + 1) {
            if (!Pattern.compile("[\u4e00-\u9fa5]").matcher(
                    String.valueOf(keyword.charAt(i))).find()) {
                return false;
            }
        }

        return true;
    }

    @Override
    public GoodsTypeVo getGoodsType(Integer storeId) {
        Integer onShelves = 0;
        Integer offShelf = 0;
        Integer auditPass = 0;
        Integer inAudit = 0;
        Integer auditFailed = 0;

        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<PmsProduct> pmsProducts = productMapper.selectList(queryWrapper);

        if (pmsProducts.size() > 0) {
            for (PmsProduct pmsProduct : pmsProducts) {

                Integer verifyStatus = pmsProduct.getVerifyStatus();
                Integer publishStatus = pmsProduct.getPublishStatus();
                if (publishStatus == 1) {
                    onShelves++;
                } else if (publishStatus == 0) {
                    offShelf++;
                } else if (verifyStatus == 0) {
                    inAudit++;
                } else if (verifyStatus == 1) {
                    auditPass++;
                } else {
                    auditFailed++;
                }
            }
        }
        GoodsTypeVo goodsTypeVo = new GoodsTypeVo();

        goodsTypeVo.setInAudit(inAudit);
        goodsTypeVo.setOffShelf(offShelf);
        goodsTypeVo.setOnShelves(onShelves);
        goodsTypeVo.setAuditPass(auditPass);
        goodsTypeVo.setAuditFailed(auditFailed);


        return goodsTypeVo;

    }

    private void findCategoryName(Long productId) {


        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("", "");
    }

    private List<PmsSkuStock> findSkustockList(String column, String value, String column1, Object value1, Integer pageIndex, Integer pageSize) {
        QueryWrapper<PmsSkuStock> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(value)) {
            queryWrapper.ge(column1, value1);
        } else {
            queryWrapper.like(column, value).ge(column1, value1);
        }
        IPage<PmsSkuStock> page = new Page<>();

        page.setSize(pageSize);
        page.setCurrent(pageIndex);

        return pmsSkuStockMapper.selectPage(page, queryWrapper).getRecords();
    }


    /**
     * 建立和插入关系表操作
     *
     * @param dao       可以操作的dao
     * @param dataList  要插入的数据
     * @param productId 建立关系的id
     */
    private void relateAndInsertList(Object dao, List dataList, Long productId) {
        try {
            if (CollectionUtils.isEmpty(dataList)) return;
            for (Object item : dataList) {
                Method setId = item.getClass().getMethod("setId", Long.class);
                setId.invoke(item, (Long) null);
                Method setProductId = item.getClass().getMethod("setProductId", Long.class);
                setProductId.invoke(item, productId);
            }
            Method insertList = dao.getClass().getMethod("saveBatch", Collection.class);
            insertList.invoke(dao, dataList);
        } catch (Exception e) {

            log.warn("创建产品出错:{}", e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

    public String fileUpload(String url) {

        CommonResult commonResult = new CommonResult();


        Map<String, Object> fileMap = new HashMap<>();
        Map<String, Object> tokenMap = new HashMap<>();


        tokenMap.put("appId", "cJG3JSr5od9HkN6HJwy9g8ffPOUmfSUFVtdkXovyt5KqZVBiOeKQz0Zp9KmVRzYl");
        tokenMap.put("appSecret", "qVw9mEy7MDK7skv8BXa1mw==");

        String token = getToken(tokenMap);

        //文件上传
        fileMap.put("token", token);
        fileMap.put("appId", "cJG3JSr5od9HkN6HJwy9g8ffPOUmfSUFVtdkXovyt5KqZVBiOeKQz0Zp9KmVRzYl");
        fileMap.put("platformId", "");
        fileMap.put("bucketName", "personal");
        fileMap.put("url", url);


        JSONObject jsonObject = agentFileUpload(fileMap);

        String data = null;
        try {
            data = jsonObject.get("data").toString();

        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }

        return data;
    }

    private String getToken(Map<String, Object> tokenMap) {
        CommonResult commonResult = new CommonResult();
        String tokenResult = HttpClientUtil.postJson("http://127.0.0.1:8084/authoriz/getToken", tokenMap);

        if (StringUtils.isEmpty(tokenResult)) {
            return "1";
        }

        JSONObject tokenJson = JSONObject.parseObject(tokenResult);

        if (!tokenJson.get("code").equals("200")) {
            return "2";
        }

        return tokenJson.get("data").toString();
    }

    private JSONObject agentFileUpload(Map<String, Object> fileMap, List<String> fileName, List<MultipartFile> fileList) {
        String fileResult = HttpClientUtil.httpPostRequest2("", fileList, fileName, fileMap, 5000);

//        if (StringUtils.isEmpty(fileResult)) {
//            throw new CustomException(ResponseUtil.fall("-9001", "系统异常"));
//        }

        //        if (!fileJson.get("code").equals("200")) {
//            throw new CustomException(ResponseUtil.fall("-9001", "系统异常"));
//        }
        return JSONObject.parseObject(fileResult);
    }

    private JSONObject agentFileUpload(Map<String, Object> fileMap) {
        String fileResult = HttpClientUtil.postJson("http://127.0.0.1:8086/file/compression", fileMap);

//        if (StringUtils.isEmpty(fileResult)) {
//            throw new CustomException(ResponseUtil.fall("-9001", "系统异常"));
//        }

        //        if (!fileJson.get("code").equals("200")) {
//            throw new CustomException(ResponseUtil.fall("-9001", "系统异常"));
//        }
        return JSONObject.parseObject(fileResult);
    }


}


